package com.example.sanfang.um;

import android.content.Intent;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.example.sanfang.R;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMVideo;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.media.UMusic;

public class SharedActivity extends AppCompatActivity implements View.OnClickListener {
    private static String TAG = SharedActivity.class.getSimpleName();

    private Button btn_wxWordShared;
    private Button btn_wxImgShared;
    private Button btn_wxUrlShared;
    private Button btn_wxAudioShared;
    private Button btn_wxVideoShared;
    private Button btn_systemShare;
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared);
        initView();
    }

    private void initView() {
        btn_wxWordShared = (Button) findViewById(R.id.btn_wxWordShared);
        btn_wxImgShared = (Button) findViewById(R.id.btn_wxImgShared);
        btn_wxUrlShared = (Button) findViewById(R.id.btn_wxUrlShared);
        btn_wxAudioShared = (Button) findViewById(R.id.btn_wxAudioShared);
        btn_wxVideoShared = (Button) findViewById(R.id.btn_wxVideoShared);
        btn_systemShare = (Button) findViewById(R.id.btn_systemShare);
        btn_login = (Button) findViewById(R.id.btn_login);

        btn_wxWordShared.setOnClickListener(this);
        btn_wxImgShared.setOnClickListener(this);
        btn_wxUrlShared.setOnClickListener(this);
        btn_wxAudioShared.setOnClickListener(this);
        btn_wxVideoShared.setOnClickListener(this);
        btn_systemShare.setOnClickListener(this);
        btn_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_wxWordShared:
                shareWord();
                break;
            case R.id.btn_wxImgShared:
                shareImg();
                break;
            case R.id.btn_wxUrlShared:
                shareUrl();
                break;
            case R.id.btn_wxAudioShared:
                shareAudio();
                break;
            case R.id.btn_wxVideoShared:
                shareVideo();
                break;
            case R.id.btn_systemShare:
                shareSystem();
                break;
        }
    }
    /**
     * 不带面板的文字分享
     */
    private void shareWord(){
        new ShareAction(SharedActivity.this)
                .setPlatform(SHARE_MEDIA.WEIXIN)//传入平台
                .withText("分享测试")//分享内容
                .setCallback(shareListener)//回调监听器
                .share();
    }

    /**
     * 图片分享
     */
    private void shareImg(){
        UMImage image = new UMImage(SharedActivity.this,"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1606061378&di=1d992ee99f3ec60e07f32825247a2894&src=http://hbimg.b0.upaiyun.com/9552f698bd60aa4c25bad463afd3d081d2415d6c3a934-IlLJQs_fw658");
        image.setTitle("图片分享");
        image.setDescription("图片分享测试");
        new ShareAction(SharedActivity.this)
                .setPlatform(SHARE_MEDIA.WEIXIN)
                .withMedia(image)
                .setCallback(shareListener)
                .share();
    }

    /**
     * 地址分享
     */
    private void shareUrl(){
        UMWeb web = new UMWeb("https://www.baidu.com");
        web.setTitle("百度");
        web.setThumb(new UMImage(SharedActivity.this,"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1606061378&di=1d992ee99f3ec60e07f32825247a2894&src=http://hbimg.b0.upaiyun.com/9552f698bd60aa4c25bad463afd3d081d2415d6c3a934-IlLJQs_fw658"));
        web.setDescription("地址分享测试");
        new ShareAction(SharedActivity.this)
                .setPlatform(SHARE_MEDIA.WEIXIN)
                .withMedia(web)
                .share();
    }

    private void shareAudio(){
        UMusic music = new UMusic("http://yun918.cn/audio/background/%E7%AB%A5%E8%AF%9D%E9%95%87.mp3");
        music.setTitle("音频分享");
        music.setThumb(new UMImage(SharedActivity.this,"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1606061378&di=1d992ee99f3ec60e07f32825247a2894&src=http://hbimg.b0.upaiyun.com/9552f698bd60aa4c25bad463afd3d081d2415d6c3a934-IlLJQs_fw658"));
        music.setDescription("音频播放");
        new ShareAction(SharedActivity.this)
                .setPlatform(SHARE_MEDIA.WEIXIN)
                .withMedia(music)
                .share();
    }

    private void shareVideo(){
        UMVideo video = new UMVideo("http://yun918.cn/video/1578640184375179.mp4");
        video.setTitle("视频分享");
        video.setThumb(new UMImage(SharedActivity.this,"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1606061378&di=1d992ee99f3ec60e07f32825247a2894&src=http://hbimg.b0.upaiyun.com/9552f698bd60aa4c25bad463afd3d081d2415d6c3a934-IlLJQs_fw658"));
        video.setDescription("视频分享播放");
        new ShareAction(SharedActivity.this)
                .setPlatform(SHARE_MEDIA.WEIXIN)
                .withMedia(video)
                .share();
    }

    private UMShareListener shareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
            Log.i(TAG,"onStart");
        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            Log.i(TAG,"onResult");
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            Log.i(TAG,"onError");
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            Log.i(TAG,"onCancel");
        }
    };

    private void shareSystem(){
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");// setType("audio/*");
        intent.putExtra(Intent.EXTRA_SUBJECT, "share");
        intent.putExtra(Intent.EXTRA_TEXT, "test http://www.qq.com/   测试");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(Intent.createChooser(intent, getTitle()));

    }

    private static String getMimeType(String filePath) {
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        String mime = "*/*";
        if (filePath != null) {
            try {
                mmr.setDataSource(filePath);
                mime = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE);
            } catch (RuntimeException e) {
                return mime;
            }
        }
        return mime;
    }
}